package com.mf.morefun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.mf.morefun.R;
import com.mf.morefun.bean.GiftBean;
import com.mf.morefun.listener.ReadDetailTypeListener;
import com.mf.morefun.widget.GlideRoundTransform;
import com.mf.morefun.widget.TransformationgiftUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import org.jetbrains.annotations.Nullable;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.support.constraint.Constraints.TAG;

public class GiftAdapter extends BaseAdapter {
    private Context mContext;
    private List<GiftBean> mGiftList;
    private ReadDetailTypeListener listener;

    public ReadDetailTypeListener getListener() {
        return listener;
    }

    public void setListener(ReadDetailTypeListener listener) {
        this.listener = listener;
    }

    public GiftAdapter(Context mContext, List<GiftBean> mGiftList) {
        this.mContext = mContext;
        this.mGiftList = mGiftList;
    }

    @Override
    public int getCount() {
        return mGiftList.size();
    }

    @Override
    public Object getItem(int position) {
        return mGiftList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_giftitem, null);
        } else {
            view = convertView;
        }
        if (null != mGiftList && mGiftList.size() > 0) {
            ViewHolder holder = new ViewHolder(view);
            ViewGroup.LayoutParams params = holder.llBg.getLayoutParams();
            if(null != params){
                params.height = DeviceUtils.dip2px(mContext, 30);
                params.width = (int) ((DeviceUtils.deviceWidth(mContext) / 3) - 30);
            }else {
                params = new ViewGroup.LayoutParams((int) ((int) ((DeviceUtils.deviceWidth(mContext) / 3) - 30)), DeviceUtils.dip2px(mContext, 30));
            }
            holder.llBg.setLayoutParams(params);
            holder.tvGiftname.setText(mGiftList.get(position).getName() + mGiftList.get(position).getIntroduce());
            holder.tvNum.setText("x" + mGiftList.get(position).getCostEnegyStone());
            RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 16));
            Glide.with(mContext).asBitmap().load(mGiftList.get(position).getPic()).listener(new mRequestListener(holder)).apply(options).into(new TransformationgiftUtils(holder.ivGiftimg, mContext));
            holder.tvExchange.setTag(position);
            holder.tvExchange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.callBack((int) v.getTag());
                }
            });
        }
        return view;
    }

    private class mRequestListener implements RequestListener {
        private ViewHolder holder;

        public mRequestListener(ViewHolder holder) {
            this.holder = holder;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
            Log.d(TAG, "onException: " + e.toString() + "  model:" + model + " isFirstResource: " + isFirstResource);
            Message msg = new Message();
            msg.what = 0;
            msg.obj = holder.ivGiftimg;
            mHandler.sendMessage(msg);
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            Log.e(TAG, "model:" + model + " isFirstResource: " + isFirstResource);
            return false;
        }
    }

    ;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 14));
                    Glide.with(mContext).asBitmap().load(R.mipmap.gift_bg).apply(options).into(new TransformationgiftUtils((ImageView) msg.obj, mContext));
                    break;
            }
        }
    };

    static class ViewHolder {
        @BindView(R.id.iv_giftimg)
        ImageView ivGiftimg;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;
        @BindView(R.id.tv_giftname)
        TextView tvGiftname;
        @BindView(R.id.tv_exchange)
        TextView tvExchange;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
